<?php

include_once '../inc/db_util.php';

function view($filter) {
    $array_announce = array();
    if (!is_null($filter)) {
        $jsArray = json_decode($filter, false);
        $query = mysql_query("SELECT * FROM tb_pengumuman tp JOIN tb_dosen td ON tp.nip = td.nip 
             WHERE id_pengumuman='" . $jsArray->{'Id'} . "'");
    } else {
        $query = mysql_query("SELECT * FROM tb_pengumuman tp JOIN tb_dosen td ON tp.nip = td.nip ORDER BY tanggal_create DESC");
    }
    while ($row = mysql_fetch_array($query, MYSQL_BOTH)) {
        $row_array['Id'] = $row['id_pengumuman'];
        $row_array['Creator']['Nip'] = $row['nip'];
        $row_array['Creator']['Nama'] = $row['namadosen'];
        $row_array['Perihal'] = $row['prihal'];
        $row_array['Info'] = $row['pengumuman'];
        $row_array['Created'] = $row['tanggal_create'];
        $row_array['ImageUrl'] = $row['image_url'];
        $row_array['Status'] = $row['setatus'];

        array_push($array_announce, $row_array);
    }
    return json_encode($array_announce);
}

function find($keyword) {
    $array_announce = array();
    $jsArray = json_decode($keyword, false);
    $query = mysql_query("SELECT * FROM tb_pengumuman tp JOIN tb_dosen td ON tp.nip = td.nip 
             WHERE prihal LIKE '%" . $jsArray->{'Info'} . "%' OR pengumuman LIKE '%" . $jsArray->{'Info'} . "%'
			 ORDER BY tanggal_create DESC");

    while ($row = mysql_fetch_array($query, MYSQL_BOTH)) {
        $row_array['Id'] = $row['id_pengumuman'];
        $row_array['Creator']['Nip'] = $row['nip'];
        $row_array['Creator']['Nama'] = $row['namadosen'];
        $row_array['Perihal'] = $row['prihal'];
        $row_array['Info'] = $row['pengumuman'];
        $row_array['Created'] = $row['tanggal_create'];
        $row_array['ImageUrl'] = $row['image_url'];
        $row_array['Status'] = $row['setatus'];

        array_push($array_announce, $row_array);
    }
    return json_encode($array_announce);
}

function create($json) {
    $jsArray = json_decode($json, false);
    $sql = "INSERT INTO tb_pengumuman (nip, prihal, pengumuman, image_url, setatus, tanggal_create)
        VALUES (
        '" . $jsArray->{'Creator'}->{'Nip'} . "',
        '" . $jsArray->{'Perihal'} . "',
        '" . $jsArray->{'Info'} . "',
        '" . $jsArray->{'ImageUrl'} . "',
        '" . $jsArray->{'Status'} . "',
        NOW())";

    return mysql_query($sql);
}

function update($json) {
    $jsArray = json_decode($json, false);
    $sql = "UPDATE tb_pengumuman SET 
        prihal='" . $jsArray->{'Perihal'} . "',
        pengumuman='" . $jsArray->{'Info'} . "',
        image_url='" . $jsArray->{'ImageUrl'} . "',
        setatus='" . $jsArray->{'Status'} . "'
        WHERE 
        id_pengumuman='" . $jsArray->{'Id'} . "'";

    return mysql_query($sql);
}

function delete($json) {
    $jsArray = json_decode($json, false);
    $sql = "DELETE FROM tb_pengumuman WHERE id_pengumuman='" . $jsArray->{'Id'} . "'";

    return mysql_query($sql);
}

?>
